package com.estar.integaration.Controller;


import cn.hutool.Hutool;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.estar.integaration.Service.*;
import com.estar.integaration.Util.MaterialUtil;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.*;
import com.estar.integaration.entity.NoSQL.Res;
import com.google.gson.Gson;
import org.apache.poi.poifs.property.Child;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YJL
 * @since 2021-11-20
 */
@RestController
@RequestMapping("/materialvo")
public class MaterialVoController {

  @Autowired
  IMaterialVoService materialVoService;
  @Autowired
  IMaterialService materialService;
  @Autowired
  IAcodeProductVoService acodeProductVoService;
  @Autowired
  IProductDbService productDbService;
  @Autowired
  IChildAcodeService childAcodeService;
  @Autowired
  IProductionScheduleService productionScheduleService;

  @GetMapping("/parseMaterialVo")
  public Res parseMaterialVo(){

    List<String> returnList = new ArrayList<>();
    //拿到没找到交期的product
    List<ProductDb> productDbList = productDbService.selectNullDeliveryProductList();
    //拿到交期hash
    HashMap<String,Date> schedulehashMap = productionScheduleService.getScheduleHash();

    for(ProductDb productDb : productDbList){
      //通过id找所有子A码
      List<ChildAcode> childAcodeList = childAcodeService.getChildACodeByProductId(productDb.getId());
      //找交期
      for(ChildAcode childAcode : childAcodeList){

        if(schedulehashMap.containsKey(childAcode.getACode())){
          //找到交期
          Date date = schedulehashMap.get(childAcode.getACode());

          productDb.setProductDeliveryTime(date);

          //写入AcodeProductVo
          AcodeProductVo acodeProductVo = new AcodeProductVo(Util.getUUID(),productDb.getACode(),productDb.getId(),date);

          //本product所有materialVO转成material置交期
          List<MaterialVo> materialVoList = materialVoService.getMaterialByProductId(productDb.getId());

          List<Material> materialList = MaterialUtil.changeAllMaterialVo2Material(materialVoList,productDb.getId(),date,acodeProductVo.getId());

          returnList.add(productDb.getACode());

          //写入数据库
          materialService.saveBatch(materialList);

          materialVoService.deleteById(materialVoList);

          acodeProductVoService.insert(acodeProductVo);

          productDbService.updateProductDbById(productDb);

          break;

        }

      }

    }

    List<MaterialVo> returnMaterialVoList = materialVoService.getMaterialVOList(null);

//    JSONArray jsonArray1 = JSONArray.parseArray(JSON.toJSONString(returnList));
//
//    JSONArray jsonArray2 = JSONArray.parseArray(JSON.toJSONString(returnMaterialVoList));
//
//    JSONObject jsonObject = new JSONObject();
//
//    jsonObject.put("materialList",jsonArray2);
//
//    jsonObject.put("returnList",jsonArray1);

    return new Res("20000",new Gson().toJson(returnMaterialVoList));

  }

  @GetMapping("/getMaterialVo")
  public Res getMaterialVoDate(){

   return  new Res("20000",new Gson().toJson(materialVoService.getMaterialVOList(null)));

  }



}
